package com.android.fastride.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.android.fastride.R;
import com.android.fastride.activity.MainActivity;
import com.android.fastride.contract.LoginContract;
import com.android.fastride.manager.UserManager;
import com.android.fastride.model.http.entity.LoginUserInfoEntity;
import com.android.fastride.present.LoginPresent;
import com.peng.library.mantis.expand.data.MantisDataFragment;
import com.peng.library.mantis.hint.HintConfig;
import com.peng.library.mantis.mvp.BindPresent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by admin on 2018/5/14.
 */
@BindPresent(LoginPresent.class)
public class LoginFragment extends MantisDataFragment<LoginContract.PRESENT, Boolean> implements
        LoginContract.VIEW {

    Unbinder unbinder;
    @BindView(R.id.et_user)
    EditText etUser;
    @BindView(R.id.et_pwd)
    EditText etPwd;

    @Override
    public void skipToHomePage() {
        MainActivity.start(getContext());
    }

    @Override
    public void onGetVerifyCodeError() {

    }

    @Override
    public void onReceiveData(Boolean aBoolean) {

    }

    @Override
    public void onReceiveError(Throwable error) {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState, HintConfig.Builder hintConfigBuilder) {
        View contentView = inflater.inflate(R.layout.fragment_login, container, false);
        unbinder = ButterKnife.bind(this, contentView);
        initData();
        return contentView;
    }

    private void initData() {
        // 从缓存中读取用户名密码并进行自动登录
        LoginUserInfoEntity userInfoEntity = UserManager.getInstance().getUserInfo();
        String userName = "";
        String password = "";
        if(userInfoEntity != null){
            userName = userInfoEntity.getPhone();
            password = userInfoEntity.getPassword();
            etUser.setText(userName);
            etPwd.setText(password);
        }

        if(!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(password)) {
            getPresent().loginByPassword(userName, password, "");
        }

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String userName = etUser.getText().toString();
        String passWord = etPwd.getText().toString();
        if (inputError(userName, passWord))
            return;
        // 登录
        getPresent().loginByPassword(userName, passWord, "");
        /*ControlSysService controlSysService = FastRideApplication.getApp().getControlSysService();
        controlSysService.loginByPwd(userName, passWord, "");
        MainActivity.start(getContext());*/
    }

    /**
     * 输入项校验
     * @param userName
     * @param passWord
     * @return
     */
    private boolean inputError(String userName, String passWord) {
        boolean result = false;
        if (TextUtils.isEmpty(userName)) {
            Toast.makeText(getContext(), getString(R.string.user_name_empty), Toast.LENGTH_LONG)
                    .show();
            result = true;
        } else if (TextUtils.isEmpty(passWord)) {
            Toast.makeText(getContext(), getString(R.string.password_empty), Toast.LENGTH_LONG)
                    .show();
            result = true;
        }
        return result;
    }
}
